﻿using System;
using System.Collections.Generic;
using DataBaseLibrary.DBInterfaces;
using System.ComponentModel;

namespace DataBaseLibrary.DBObjectsFB
{
    public class Vu : DBFObject, IDBObject, INotifyPropertyChanged
    {

        private void OnPropertyChanged(PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, e);
        }




        /// <summary>
        /// Дата выдачи
        /// </summary>
        private DateTime? m_DateIssue = null;

        public DateTime? DateIssue
        {
            get { return m_DateIssue; }
            set
            {
                m_DateIssue = value;
                OnPropertyChanged(new PropertyChangedEventArgs("DateIssue"));
            }
        }


        /// <summary>
        /// Категория
        /// </summary>
        private String m_Category = "";

        public string Category
        {
            get { return m_Category; }
            set
            {
                m_Category = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Category"));
            }
        }

        /// <summary>
        /// Номер
        /// </summary>
        private String m_Number = "";

        public string Number
        {
            get { return m_Number; }
            set
            {
                m_Number = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Number"));
            }
        }


        /// <summary>
        /// Серия
        /// </summary>
        private String m_Series = "";

        public string Series
        {
            get { return m_Series; }
            set
            {
                m_Series = value;
                OnPropertyChanged(new PropertyChangedEventArgs("Series"));
            }
        }


        /// <summary>
        /// Кем вадано
        /// </summary>
        private String m_IssuedBy = "";

        public string IssuedBy
        {
            get { return m_IssuedBy; }
            set
            {
                OnPropertyChanged(new PropertyChangedEventArgs("IssuedBy"));
                m_IssuedBy = value;
            }
        }


        public override string ToString()
        {
            return "Заявление на ВУ";
        }


        public Vu()
        {
            NameTable = "VU_TABLE";

            DateIssue = null;
            Category = "";
            Number = "";
            Series = "";
            IssuedBy = "";

            
        }


        private void CreateData()
        {
            Parameters = new Dictionary<string, object>();
            Parameters.Add("DATE_ISSUE", DateIssue);
            Parameters.Add("CATEGORY", Category);
            Parameters.Add("NUMBER_VU", Number);
            Parameters.Add("SERIES", Series);
            Parameters.Add("ISSUED_BY", IssuedBy);
            
        }



        public object Save()
        {
            try
            {
                CreateData();
                return SaveObject();
            }
            catch (SystemException)
            {
                throw new SystemException(String.Format("Ошибка при сохранении объекта {0} в БД", this.ToString()));
            }
        }

        public void Load(object ID)
        {
            try
            {
                CreateData();
                LoadObject(ID);

                DateIssue = Parameters["DATE_ISSUE"] == null || Parameters["DATE_ISSUE"].ToString() == String.Empty
                    ? (DateTime?) null
                    : DateTime.Parse(Parameters["DATE_ISSUE"].ToString());

                Category = Parameters["CATEGORY"].ToString();
                Number = Parameters["NUMBER_VU"].ToString();
                Series = Parameters["SERIES"].ToString();
                IssuedBy = Parameters["ISSUED_BY"].ToString();
                
            }
            catch (SystemException)
            {
                throw new SystemException(String.Format("Ошибка при загрузке объекта {0} в БД", this.ToString()));
            }
        }

        public void Update()
        {
            CreateData();
            UpdateObject();
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}
